import 'package:flutter/services.dart';
import 'package:manual/utils/download_image.dart';
import 'package:manual/utils/logger.dart';


class MethodChannels {
  static const gallerySaverChannel = MethodChannel('native_gallery_saver');

   static Future<bool> saveToGallery(String type) async {
    late String? filePath; // 使用 late 声明变量（延迟初始化）
    if (type == 'local') {
      filePath = await DownloadImage.copyAssetToFilePath("lib/assets/images/common/icon.png");
    } else {
      filePath = await DownloadImage.downloadImage("https://img0.baidu.com/it/u=2281553882,682107731&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=667");
    }
    try {
      final result = await gallerySaverChannel.invokeMethod('saveToGallery', {'filePath': filePath});
      Logger.info("保存图片成功$result" , tag: 'saveToGallery');
      return result;
      
    } catch (e) {
      Logger.error("保存图片出错$e" , tag: 'saveToGallery');
      return false;
    }
  }
}